
































package com.kayang.common;

import com.kayang.common.exception.ConfigException;
import com.kayang.common.exception.IPException;
import com.kayang.common.exception.ParamException;
import com.kayang.common.exception.SPserviceException;
import com.kayang.common.languages.MSGAlarm;
import com.kayang.nadapt.util.JsonConvert;
import com.kayang.nadapt.util.LogHelper;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;
import java.util.Map;

@RestControllerAdvice
public class CustomExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public String exceprion(Exception ex){
        LogHelper.error(ex);
        HttpMsg msg = new HttpMsg();
        msg.setMsgId(-99);
        msg.setMsg(ex.getMessage());
        msg.setResult("Failed");
        return JsonConvert.serializeObject(msg);
    }


    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseEntity<HttpMsg> handleValidationExceptions(MethodArgumentNotValidException ex) {
        LogHelper.error(ex);
        HttpMsg msg = new HttpMsg();
        msg.setMsgId(-1);
        Map<String, String> errors = new HashMap<>();
        ex.getBindingResult().getAllErrors().forEach((error) -> {
            String fieldName = ((FieldError) error).getField();
            String errorMessage = error.getDefaultMessage();
            errors.put(fieldName, errorMessage);
        });
        msg.setMsg(ex.getMessage());
        msg.setResult(errors);
        return new ResponseEntity<>(msg, HttpStatus.BAD_REQUEST);
    }

    @ExceptionHandler(value = ParamException.class)
    public String paramExceprion(ParamException ex){
        LogHelper.error(ex);
        HttpMsg msg = new HttpMsg();
        msg.setMsgId(ex.getMsgid());
        msg.setMsg("Failed");
        msg.setResult(ex.getMsg());
        return JsonConvert.serializeObject(msg);
    }

    @ExceptionHandler(value = IPException.class)
    public String ipExceprion(IPException ex){
        LogHelper.error(ex);
        HttpMsg msg = new HttpMsg();
        msg.setMsgId(ex.getMsgid());
        msg.setMsg(ex.getMsg());
        msg.setResult("Failed");
        return JsonConvert.serializeObject(msg);
    }

    @ExceptionHandler(value = ConfigException.class)
    public String configException(ConfigException ex){
        LogHelper.error(ex);
        HttpMsg msg = new HttpMsg();
        msg.setMsgId(ex.getMsgid());
        msg.setMsg(ex.getMsg());
        msg.setResult("Failed");
        return JsonConvert.serializeObject(msg);
    }

    @ExceptionHandler(value = CommonException.class)
    public String otherException(CommonException ex){
        LogHelper.error(ex);
        HttpMsg msg = new HttpMsg();
        msg.setMsgId(-1);
        msg.setMsg(ex.getMsg());
        msg.setResult("Failed");
        return JsonConvert.serializeObject(msg);
    }

    @ExceptionHandler(value = SPserviceException.class)
    public String otherException(SPserviceException ex){
        LogHelper.error(ex);
        MSGAlarm alarm = ex.getMSGAlarm();
        HttpMsg msg = new HttpMsg();
        msg.setMsgId(alarm.getID());
        msg.setMsg(alarm.getTitle());
        msg.setResult("执行自定义业务存在警告或错误");
        return JsonConvert.serializeObject(msg);
    }


}
